﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using School.Common;

namespace School.Service.BOOKS
{
    public class SRV_BOOKS
    {
        private static SchoolEntities db = new SchoolEntities();
        public static IEnumerable<SRV_Book> AllBooks()
        {
            var query = from Book in db.SRV_Book select Book;
            return query.ToList();
        }
        public static IEnumerable<SRV_Book> GetById(int id)
        {
            var query = from book in db.SRV_Book
                        where book.Id == id
                        select book;
            return query.ToList();
        }
        public static bool BooksInsert(SRV_Book book, out long bookId)
        {

            if (book.Grad_Id == 0)
                book.Grad_Id = null;
            if (book.Cer_Id == 0)
                book.Cer_Id = null;
            if (book.Stage_Id == 0)
                book.Stage_Id = null;
            db.AddToSRV_Book(book);
            try
            {
                db.SaveChanges();
                bookId = book.Id;
                return true;
            }
            catch
            {
                bookId = 0;
                return false;
            }
        }
        public static bool BooksUpdate(SRV_Book BookToUpdate)
        {
            db.SRV_Book.Where(s => s.Id == BookToUpdate.Id).FirstOrDefault().Code = BookToUpdate.Code;

            var query = (from book in db.SRV_Book
                         where book.Id == BookToUpdate.Id
                         select book).Single();
            query.Name = BookToUpdate.Name;
            query.Description = BookToUpdate.Description;
            query.Cer_Id = BookToUpdate.Cer_Id;
            query.Campus_Id = BookToUpdate.Campus_Id;
            query.Grad_Id = BookToUpdate.Grad_Id;
            query.Stage_Id = BookToUpdate.Stage_Id;
            //query.Code = ClotheToUpdate.Code;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool BookDelete(long id)
        {
            var query = (from book in db.SRV_Book
                         where book.Id == id
                         select book).Single();

            db.SRV_Book.DeleteObject(query);
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool BooksUpdate(List<SRV_Book> BooksToUpdate)
        {
            foreach (SRV_Book item in BooksToUpdate)
            {
                db.SRV_Book.Where(s => s.Id == item.Id).First().BookQty = item.BookQty;
            }
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IQueryable<SRV_Book> BookStockReport(int campusid, int cerId, int StageId, int GradeId)
        {
            var query = from book in db.SRV_Book
                        where book.Campus_Id == campusid && book.Cer_Id == cerId
                        && book.Stage_Id == StageId && book.Grad_Id == GradeId
                        select book;
            return query;
        }
        public static IQueryable<ReportStockBookData> SRV_Book_STOCKTACKINGSearch(DateTime date)
        {
            var query = from stock in db.vw_SrvBookStachTacking
                        where stock.Date_Time == date
                        select new ReportStockBookData
                        {
                            Book_Code = stock.Code.TrimEnd(),
                            BookName = stock.Name.TrimEnd(),
                            SystemQty = stock.System_Qty,
                            Pysical_Qty = stock.Pysical_Qty,
                            Differance = stock.Pysical_Qty - stock.System_Qty,
                            Equating_Date_Time = stock.Equating_Date_Time != null ? (DateTime)stock.Equating_Date_Time : (DateTime?)null
                        };
            return query;

        }
        public static IEnumerable<object> getAllGrd(long campId, long CerId, long StageId, long GradeId)
        {
            var query =
               from list in db.SRV_Book
               where list.Campus_Id == campId && list.Cer_Id == CerId && list.Grad_Id == GradeId &&
               list.Stage_Id == StageId
               select new
               {
                   Id = list.Id,
                   Name = list.Name,
                   CampusName = list.ORG_Campus.Name,
                   CertificateName = list.ORG_Certificate != null ? (string)list.ORG_Certificate.Name : "",
                   StageName = list.ORG_Stage != null ? (string)list.ORG_Stage.Name : "",
                   GradeName = list.ORG_Grade != null ? (string)list.ORG_Grade.Name : "",
                   Code = list.Code,
               };
            return query;
        }

        public static IEnumerable<OrderPayPrintData> PrintOrder(string stu_Name, string CamName, string CerName,
          string StageName, string GradeName, long PayNum, decimal Price)
        {
            List<OrderPayPrintData> lst = new List<OrderPayPrintData>();
            lst.Add(new OrderPayPrintData()
            {
                StudentName = stu_Name,
                CamName = CamName,
                CerName = CerName,
                StageName = StageName,
                GradeName = GradeName,
                PayNum = PayNum,
                Price = Price
            });
            return lst;
        }
        public static IEnumerable<GrdObjectBook> PrintOrderDataGrd(List<GrdObjectBook> GrdLst)
        {
            return GrdLst;
        }
        public static IEnumerable<DiscountObject> PrintOrderDiscount(List<DiscountObject> DiscountLst)
        {
            return DiscountLst;
        }

    }

    public class GrdObjectBook
    {
        public int campusId;
        public int CampusId
        {
            get
            {
                return campusId;
            }
            set
            {
                campusId = value;
            }
        }

        public int studentId;
        public int StudentId
        {
            get
            {
                return studentId;
            }
            set
            {
                studentId = value;
            }
        }

        private string campusName;
        public string CampusName
        {
            get
            {
                return campusName;
            }
            set
            {
                campusName = value;
            }
        }

        public int cerId;
        public int CerId
        {
            get
            {
                return cerId;
            }
            set
            {
                cerId = value;
            }
        }

        private string cerName;
        public string CerName
        {
            get
            {
                return cerName;
            }
            set
            {
                cerName = value;
            }
        }

        public int stageId;
        public int StageId
        {
            get
            {
                return stageId;
            }
            set
            {
                stageId = value;
            }
        }

        private string stageName;
        public string StageName
        {
            get
            {
                return stageName;
            }
            set
            {
                stageName = value;
            }
        }

        public int gadeId;
        public int GradeId
        {
            get
            {
                return gadeId;
            }
            set
            {
                gadeId = value;
            }
        }

        private string gradeName;
        public string GradeName
        {
            get
            {
                return gradeName;
            }
            set
            {
                gradeName = value;
            }
        }
     

        public int bookId;
        public int BookId
        {
            get
            {
                return bookId;
            }
            set
            {
                bookId = value;
            }
        }

        private string bookName;
        public string BookName
        {
            get
            {
                return bookName;
            }
            set
            {
                bookName = value;
            }
        }

        public long priceId;
        public long PriceId
        {
            get
            {
                return priceId;
            }
            set
            {
                priceId = value;
            }
        }

        private double priceName;
        public double PriceName
        {
            get
            {
                return priceName;
            }
            set
            {
                priceName = value;
            }
        }

        public int userQty;
        public int UserQty
        {
            get
            {
                return userQty;
            }
            set
            {
                userQty = value;
            }
        }

        private double totalPrice;
        public double TotalPrice
        {
            get
            {
                return totalPrice;
            }
            set
            {
                totalPrice = value;
            }
        }

        private double discountPercent;
        public double DiscountPercent
        {
            get
            {
                return discountPercent;
            }
            set
            {
                discountPercent = value;
            }
        }

        //public int DiscountId;
        private double priceWzDiscount;
        public double PriceWzDiscount
        {
            get
            {
                return priceWzDiscount;
            }
            set
            {
                priceWzDiscount = value;
            }
        }
    }
    public class ReportStockBookData
    {
        public DateTime? _Equating_Date_Time;
        public DateTime? Equating_Date_Time
        {
            get
            {
                return _Equating_Date_Time;
            }
            set
            {
                _Equating_Date_Time = value;
            }
        }

        public long _Differance;
        public long Differance
        {
            get
            {
                return _Differance;
            }
            set
            {
                _Differance = value;
            }
        }

        public long _Pysical_Qty;
        public long Pysical_Qty
        {
            get
            {
                return _Pysical_Qty;
            }
            set
            {
                _Pysical_Qty = value;
            }
        }

        public long _SystemQty;
        public long SystemQty
        {
            get
            {
                return _SystemQty;
            }
            set
            {
                _SystemQty = value;
            }
        }

        public string _BookName;
        public string BookName
        {
            get
            {
                return _BookName;
            }
            set
            {
                _BookName = value;
            }
        }

        public string _Book_Code;
        public string Book_Code
        {
            get
            {
                return _Book_Code;
            }
            set
            {
                _Book_Code = value;
            }
        }
    }
    public class OrderPayPrintData
    {
        public string StudentName { get; set; }
        public string CamName { get; set; }
        public string CerName { get; set; }
        public string StageName { get; set; }
        public string GradeName { get; set; }
        public long PayNum { get; set; }
        public decimal Price { get; set; }
    }
}
